<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li class="active"><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. 可共享可变状态是万恶之源">1. 可共享可变状态是万恶之源</a></li><li><a href="#t12. 什么是 Immutable">2. 什么是 Immutable</a></li><li><a href="#t23. Immutable类库">3. Immutable类库</a><ul><li><a href="#t33.1 Map">3.1 Map</a></li><li><a href="#t43.2 List">3.2 List</a></li></ul></li><li><a href="#t54. Immutable优势">4. Immutable优势</a><ul><li><a href="#t64.1  降低复杂度">4.1  降低复杂度</a></li><li><a href="#t74.2 节省内存">4.2 节省内存</a></li><li><a href="#t84.3 方便回溯">4.3 方便回溯</a></li></ul></li><li><a href="#t95. React性能优化">5. React性能优化</a><ul><li><a href="#t105.1 计数器">5.1 计数器</a></li><li><a href="#t115.2 深度克隆">5.2 深度克隆</a></li><li><a href="#t125.3 深比较">5.3 深比较</a></li><li><a href="#t135.4 immutable">5.4 immutable</a></li></ul></li><li><a href="#t146. redux+immutable">6. redux+immutable</a></li><li><a href="#t157. redux-immutable">7. redux-immutable</a></li><li><a href="#t168. react-router-redux">8. react-router-redux</a></li><li><a href="#t179. react-router-redux">9. react-router-redux</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. &#x53EF;&#x5171;&#x4EAB;&#x53EF;&#x53D8;&#x72B6;&#x6001;&#x662F;&#x4E07;&#x6076;&#x4E4B;&#x6E90;">1. &#x53EF;&#x5171;&#x4EAB;&#x53EF;&#x53D8;&#x72B6;&#x6001;&#x662F;&#x4E07;&#x6076;&#x4E4B;&#x6E90; <a href="#t01. &#x53EF;&#x5171;&#x4EAB;&#x53EF;&#x53D8;&#x72B6;&#x6001;&#x662F;&#x4E07;&#x6076;&#x4E4B;&#x6E90;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> objA = { <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span> };
<span class="hljs-keyword">let</span> objB = objA;
objB.name = <span class="hljs-string">&apos;9&apos;</span>;
<span class="hljs-built_in">console</span>.log(objA.name);
</code></pre>
<h2 id="t12. &#x4EC0;&#x4E48;&#x662F; Immutable">2. &#x4EC0;&#x4E48;&#x662F; Immutable <a href="#t12. &#x4EC0;&#x4E48;&#x662F; Immutable"> # </a></h2>
<ul>
<li>Immutable Data &#x5C31;&#x662F;&#x4E00;&#x65E6;&#x521B;&#x5EFA;&#xFF0C;&#x5C31;&#x4E0D;&#x80FD;&#x518D;&#x88AB;&#x66F4;&#x6539;&#x7684;&#x6570;&#x636E;&#x3002;&#x5BF9; Immutable &#x5BF9;&#x8C61;&#x7684;&#x4EFB;&#x4F55;&#x4FEE;&#x6539;&#x6216;&#x6DFB;&#x52A0;&#x5220;&#x9664;&#x64CD;&#x4F5C;&#x90FD;&#x4F1A;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x7684; Immutable &#x5BF9;&#x8C61;</li>
<li>Immutable &#x5B9E;&#x73B0;&#x7684;&#x539F;&#x7406;&#x662F; Persistent Data Structure&#xFF08;&#x6301;&#x4E45;&#x5316;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF09;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x4F7F;&#x7528;&#x65E7;&#x6570;&#x636E;&#x521B;&#x5EFA;&#x65B0;&#x6570;&#x636E;&#x65F6;&#xFF0C;&#x8981;&#x4FDD;&#x8BC1;&#x65E7;&#x6570;&#x636E;&#x540C;&#x65F6;&#x53EF;&#x7528;&#x4E14;&#x4E0D;&#x53D8;
&#x540C;&#x65F6;&#x4E3A;&#x4E86;&#x907F;&#x514D; deepCopy &#x628A;&#x6240;&#x6709;&#x8282;&#x70B9;&#x90FD;&#x590D;&#x5236;&#x4E00;&#x904D;&#x5E26;&#x6765;&#x7684;&#x6027;&#x80FD;&#x635F;&#x8017;</li>
<li>Immutable &#x4F7F;&#x7528;&#x4E86; Structural Sharing&#xFF08;&#x7ED3;&#x6784;&#x5171;&#x4EAB;&#xFF09;&#xFF0C;&#x5373;&#x5982;&#x679C;&#x5BF9;&#x8C61;&#x6811;&#x4E2D;&#x4E00;&#x4E2A;&#x8282;&#x70B9;&#x53D1;&#x751F;&#x53D8;&#x5316;&#xFF0C;&#x53EA;&#x4FEE;&#x6539;&#x8FD9;&#x4E2A;&#x8282;&#x70B9;&#x548C;&#x53D7;&#x5B83;&#x5F71;&#x54CD;&#x7684;&#x7236;&#x8282;&#x70B9;&#xFF0C;&#x5176;&#x5B83;&#x8282;&#x70B9;&#x5219;&#x8FDB;&#x884C;&#x5171;&#x4EAB;</li>
<li><a href="http://facebook.github.io/immutable-js/docs/#/">immutable-js</a> </li>
</ul>
<p><a href="http://img.zhufengpeixun.cn/immutablejs.gif">immutablejs</a></p>
<h2 id="t23. Immutable&#x7C7B;&#x5E93;">3. Immutable&#x7C7B;&#x5E93; <a href="#t23. Immutable&#x7C7B;&#x5E93;"> # </a></h2>
<p>&#x5185;&#x90E8;&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x5957;&#x5B8C;&#x6574;&#x7684; Persistent Data Structure,&#x8FD8;&#x6709;&#x5F88;&#x591A;&#x6613;&#x7528;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x3002;&#x50CF; <code>Collection</code>&#x3001;<code>List</code>&#x3001;<code>Map</code>&#x3001;<code>Set</code>&#x3001;<code>Record</code>&#x3001;<code>Seq</code></p>
<h3 id="t33.1 Map">3.1 Map <a href="#t33.1 Map"> # </a></h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x65B9;&#x6CD5;</th>
<th style="text-align:left">&#x4F5C;&#x7528;</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">isMap</td>
<td style="text-align:left">&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;Map</td>
</tr>
<tr>
<td style="text-align:left">clear</td>
<td style="text-align:left">&#x6E05;&#x7A7A;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">set</td>
<td style="text-align:left">&#x8BBE;&#x7F6E;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">delete</td>
<td style="text-align:left">&#x5220;&#x9664;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">update</td>
<td style="text-align:left">&#x66F4;&#x65B0;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">merge</td>
<td style="text-align:left">&#x5408;&#x5E76;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">setIn</td>
<td style="text-align:left">&#x8BBE;&#x7F6E;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">deleteIn</td>
<td style="text-align:left">&#x5220;&#x9664;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">updateIn</td>
<td style="text-align:left">&#x66F4;&#x65B0;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">mergeIn</td>
<td style="text-align:left">&#x5408;&#x5E76;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">get</td>
<td style="text-align:left">&#x83B7;&#x53D6;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">getIn</td>
<td style="text-align:left">&#x83B7;&#x53D6;&#x503C;</td>
</tr>
<tr>
<td style="text-align:left">keys</td>
<td style="text-align:left">key&#x7684;&#x6570;&#x7EC4;</td>
</tr>
<tr>
<td style="text-align:left">values</td>
<td style="text-align:left">value&#x7684;&#x6570;&#x7EC4;</td>
</tr>
<tr>
<td style="text-align:left">entries</td>
<td style="text-align:left">entry&#x7684;&#x6570;&#x7EC4;</td>
</tr>
<tr>
<td style="text-align:left">toJS</td>
<td style="text-align:left">&#x8F6C;&#x6210;&#x666E;&#x901A;JS&#x5BF9;&#x8C61;</td>
</tr>
<tr>
<td style="text-align:left">toObject</td>
<td style="text-align:left">&#x8F6C;&#x6210;&#x666E;&#x901A;&#x5BF9;&#x8C61;</td>
</tr>
<tr>
<td style="text-align:left">toJSON</td>
<td style="text-align:left">&#x8F6C;&#x6210;JSON&#x5BF9;&#x8C61;</td>
</tr>
<tr>
<td style="text-align:left">toArray</td>
<td style="text-align:left">&#x8F6C;&#x6210;&#x6570;&#x7EC4;</td>
</tr>
</tbody>
</table>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> obj1 = immutable.Map({ <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">8</span> });
<span class="hljs-keyword">let</span> obj2 = obj1.set(<span class="hljs-string">&apos;name&apos;</span>, <span class="hljs-string">&apos;zfpx2&apos;</span>);
<span class="hljs-keyword">let</span> obj3 = obj2.update(<span class="hljs-string">&apos;age&apos;</span>, x =&gt; x + <span class="hljs-number">1</span>);
<span class="hljs-keyword">let</span> obj4 = obj3.merge({ <span class="hljs-attr">home</span>: <span class="hljs-string">&apos;&#x5317;&#x4EAC;&apos;</span> });
<span class="hljs-built_in">console</span>.log(obj1, obj2, obj3, obj4);


<span class="hljs-keyword">let</span> obj6 = immutable.fromJS({ <span class="hljs-attr">user</span>: { <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">8</span> }, <span class="hljs-string">&apos;k&apos;</span>: <span class="hljs-string">&apos;v&apos;</span> });
<span class="hljs-keyword">let</span> obj7 = obj6.setIn([<span class="hljs-string">&apos;user&apos;</span>, <span class="hljs-string">&apos;name&apos;</span>], <span class="hljs-string">&apos;zfpx2&apos;</span>);
<span class="hljs-keyword">let</span> obj8 = obj7.updateIn([<span class="hljs-string">&apos;user&apos;</span>, <span class="hljs-string">&apos;age&apos;</span>], x =&gt; x + <span class="hljs-number">1</span>);
<span class="hljs-keyword">let</span> obj9 = obj8.mergeIn([<span class="hljs-string">&quot;user&quot;</span>], { <span class="hljs-attr">home</span>: <span class="hljs-string">&apos;&#x5317;&#x4EAC;&apos;</span> });
<span class="hljs-built_in">console</span>.log(obj6, obj7, obj8, obj9);

<span class="hljs-built_in">console</span>.log(obj6.get(<span class="hljs-string">&apos;user&apos;</span>));

<span class="hljs-built_in">console</span>.log(obj6.getIn([<span class="hljs-string">&apos;user&apos;</span>, <span class="hljs-string">&apos;name&apos;</span>]));
<span class="hljs-built_in">console</span>.log(...obj6.keys());
<span class="hljs-built_in">console</span>.log(...obj6.values());
<span class="hljs-built_in">console</span>.log(...obj6.entries());

<span class="hljs-keyword">var</span> map1 = immutable.Map({ <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">9</span> });
<span class="hljs-keyword">var</span> map2 = immutable.Map({ <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">9</span> });
assert(map1 !== map2);
assert(<span class="hljs-built_in">Object</span>.is(map1, map2) === <span class="hljs-literal">false</span>);
assert(immutable.is(map1, map2) === <span class="hljs-literal">true</span>); 
</code></pre>
<h3 id="t43.2 List">3.2 List <a href="#t43.2 List"> # </a></h3>
<table>
<thead>
<tr>
<th style="text-align:left">&#x65B9;&#x6CD5;</th>
<th style="text-align:left">&#x4F5C;&#x7528;</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">isList</td>
<td style="text-align:left">&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;List</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">size</td>
<td style="text-align:left">&#x7EDF;&#x8BA1;&#x4E2A;&#x6570;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">push</td>
<td style="text-align:left">&#x6DFB;&#x52A0;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">pop</td>
<td style="text-align:left">&#x5F39;&#x51FA;&#x6700;&#x540E;&#x4E00;&#x4E2A;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">update</td>
<td style="text-align:left">&#x66F4;&#x65B0;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">delete</td>
<td style="text-align:left">&#x5220;&#x9664;&#x6307;&#x5B9A;&#x5143;&#x7D20;&#x7684;&#x6570;&#x7EC4;</td>
<td>delete(2)</td>
</tr>
<tr>
<td style="text-align:left">insert</td>
<td style="text-align:left">&#x63D2;&#x5165;&#x6307;&#x5B9A;&#x5143;&#x7D20;&#x7684;&#x6570;&#x7EC4;</td>
<td>insert(2)</td>
</tr>
<tr>
<td style="text-align:left">clear</td>
<td style="text-align:left">&#x6E05;&#x7A7A;&#x6570;&#x7EC4;</td>
<td>clear()</td>
</tr>
<tr>
<td style="text-align:left">concat</td>
<td style="text-align:left">&#x5408;&#x5E76;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">map</td>
<td style="text-align:left">&#x6620;&#x5C04;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">filter</td>
<td style="text-align:left">&#x8FC7;&#x6EE4;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">get</td>
<td style="text-align:left">&#x83B7;&#x53D6;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">find</td>
<td style="text-align:left">&#x67E5;&#x627E;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">includes</td>
<td style="text-align:left">&#x5224;&#x65AD;&#x5305;&#x542B;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">last</td>
<td style="text-align:left">&#x6700;&#x540E;&#x4E00;&#x4E2A;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">reduce</td>
<td style="text-align:left">&#x8BA1;&#x7B97;&#x603B;&#x548C;</td>
<td></td>
</tr>
<tr>
<td style="text-align:left">count</td>
<td style="text-align:left">&#x7EDF;&#x8BA1;&#x4E2A;&#x6570;</td>
</tr>
</tbody>
</table>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> immutable = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;immutable&apos;</span>);
<span class="hljs-keyword">let</span> arr1 = immutable.fromJS([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]);
<span class="hljs-built_in">console</span>.log(arr1.size);
<span class="hljs-keyword">let</span> arr2 = arr1.push(<span class="hljs-number">4</span>);
<span class="hljs-built_in">console</span>.log(arr2);
<span class="hljs-keyword">let</span> arr3 = arr2.pop();
<span class="hljs-built_in">console</span>.log(arr3);
<span class="hljs-keyword">let</span> arr4 = arr3.update(<span class="hljs-number">2</span>, x =&gt; x + <span class="hljs-number">1</span>);
<span class="hljs-built_in">console</span>.log(arr4);
<span class="hljs-keyword">let</span> arr5 = arr4.concat([<span class="hljs-number">5</span>, <span class="hljs-number">6</span>]);
<span class="hljs-built_in">console</span>.log(arr5);
<span class="hljs-keyword">let</span> arr6 = arr5.map(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item * <span class="hljs-number">2</span>);
<span class="hljs-built_in">console</span>.log(arr6);
<span class="hljs-keyword">let</span> arr7 = arr6.filter(<span class="hljs-function"><span class="hljs-params">item</span> =&gt;</span> item &gt;= <span class="hljs-number">10</span>);
<span class="hljs-built_in">console</span>.log(arr7);
<span class="hljs-built_in">console</span>.log(arr7.get(<span class="hljs-number">0</span>));
<span class="hljs-built_in">console</span>.log(arr7.includes(<span class="hljs-number">10</span>));
<span class="hljs-built_in">console</span>.log(arr7.last());
<span class="hljs-keyword">let</span> val = arr7.reduce(<span class="hljs-function">(<span class="hljs-params">val, item</span>) =&gt;</span> val + item, <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(val);
<span class="hljs-built_in">console</span>.log(arr7.count());

<span class="hljs-comment">// 0 1 2 3 4 5  =&gt; 3 4 5 =&gt; 6 8 10 =&gt; 6 8 10 =&gt; 8 10 =&gt; 18</span>
<span class="hljs-keyword">let</span> ret = immutable.Range(<span class="hljs-number">1</span>, <span class="hljs-number">6</span>).skip(<span class="hljs-number">3</span>).map(<span class="hljs-function">(<span class="hljs-params">n</span>) =&gt;</span> n * <span class="hljs-number">2</span>).filter(<span class="hljs-function">(<span class="hljs-params">n</span>) =&gt;</span> n % <span class="hljs-number">2</span> == <span class="hljs-number">0</span>).take(<span class="hljs-number">2</span>).reduce(<span class="hljs-function">(<span class="hljs-params">a, b</span>) =&gt;</span> a + b, <span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(ret);
</code></pre>
<h2 id="t54. Immutable&#x4F18;&#x52BF;">4. Immutable&#x4F18;&#x52BF; <a href="#t54. Immutable&#x4F18;&#x52BF;"> # </a></h2>
<h3 id="t64.1  &#x964D;&#x4F4E;&#x590D;&#x6742;&#x5EA6;">4.1  &#x964D;&#x4F4E;&#x590D;&#x6742;&#x5EA6; <a href="#t64.1  &#x964D;&#x4F4E;&#x590D;&#x6742;&#x5EA6;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> obj = {<span class="hljs-attr">age</span>:<span class="hljs-number">8</span>};
handle(obj);
<span class="hljs-built_in">console</span>.log(obj.age);
</code></pre>
<h3 id="t74.2 &#x8282;&#x7701;&#x5185;&#x5B58;">4.2 &#x8282;&#x7701;&#x5185;&#x5B58; <a href="#t74.2 &#x8282;&#x7701;&#x5185;&#x5B58;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> Immutable=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;immutable&apos;</span>);
<span class="hljs-keyword">let</span> p1=Immutable.fromJS({
    <span class="hljs-attr">name</span>: <span class="hljs-string">&apos;zfpx&apos;</span>,
    <span class="hljs-attr">home</span>:{<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;beijing&apos;</span>}
});
<span class="hljs-keyword">let</span> p2 = p1.set(<span class="hljs-string">&apos;name&apos;</span>,<span class="hljs-string">&apos;zfpx2&apos;</span>);
<span class="hljs-built_in">console</span>.log(p1.get(<span class="hljs-string">&apos;home&apos;</span>)== p2.get(<span class="hljs-string">&apos;home&apos;</span>));
</code></pre>
<h3 id="t84.3 &#x65B9;&#x4FBF;&#x56DE;&#x6EAF;">4.3 &#x65B9;&#x4FBF;&#x56DE;&#x6EAF; <a href="#t84.3 &#x65B9;&#x4FBF;&#x56DE;&#x6EAF;"> # </a></h3>
<p>&#x53EA;&#x8981;&#x628A;&#x6BCF;&#x6B21;&#x7684;&#x72B6;&#x6001;&#x90FD;&#x653E;&#x5728;&#x4E00;&#x4E2A;&#x6570;&#x7EC4;&#x4E2D;&#x5C31;&#x53EF;&#x4EE5;&#x5F88;&#x65B9;&#x4FBF;&#x7684;&#x5B9E;&#x73B0;&#x64A4;&#x9500;&#x91CD;&#x505A;&#x529F;&#x80FD;</p>
<h2 id="t95. React&#x6027;&#x80FD;&#x4F18;&#x5316;">5. React&#x6027;&#x80FD;&#x4F18;&#x5316; <a href="#t95. React&#x6027;&#x80FD;&#x4F18;&#x5316;"> # </a></h2>
<h3 id="t105.1 &#x8BA1;&#x6570;&#x5668;">5.1 &#x8BA1;&#x6570;&#x5668; <a href="#t105.1 &#x8BA1;&#x6570;&#x5668;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> _ <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;lodash&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Caculator</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    state = {
        <span class="hljs-attr">number</span>: <span class="hljs-number">0</span>
    }
    handleClick = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-keyword">let</span> amount = <span class="hljs-keyword">this</span>.amount.value ? <span class="hljs-built_in">Number</span>(<span class="hljs-keyword">this</span>.amount.value) : <span class="hljs-number">0</span>;
        <span class="hljs-keyword">this</span>.state.number = <span class="hljs-keyword">this</span>.state.number + amount;
        <span class="hljs-keyword">this</span>.setState(<span class="hljs-keyword">this</span>.state);
    }
    shouldComponentUpdate(nextProps, prevState) {
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
    }
    render() {
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;render&apos;</span>);
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.state.number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{input</span> =&gt;</span> this.amount = input} /&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.handleClick}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        )
    }
}


ReactDOM.render(
    <span class="hljs-tag">&lt;<span class="hljs-name">Caculator</span> /&gt;</span>,
    document.getElementById(&apos;root&apos;)
)
</span></code></pre>
<h3 id="t115.2 &#x6DF1;&#x5EA6;&#x514B;&#x9686;">5.2 &#x6DF1;&#x5EA6;&#x514B;&#x9686; <a href="#t115.2 &#x6DF1;&#x5EA6;&#x514B;&#x9686;"> # </a></h3>
<pre><code class="lang-js">  handleClick = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-keyword">let</span> amount = <span class="hljs-keyword">this</span>.amount.value ? <span class="hljs-built_in">Number</span>(<span class="hljs-keyword">this</span>.amount.value) : <span class="hljs-number">0</span>;
        <span class="hljs-keyword">let</span> state = _.cloneDeep(<span class="hljs-keyword">this</span>.state);
        state.number = <span class="hljs-keyword">this</span>.state.number + amount;
        <span class="hljs-keyword">this</span>.setState(state);
    }
</code></pre>
<h3 id="t125.3 &#x6DF1;&#x6BD4;&#x8F83;">5.3 &#x6DF1;&#x6BD4;&#x8F83; <a href="#t125.3 &#x6DF1;&#x6BD4;&#x8F83;"> # </a></h3>
<pre><code class="lang-js"> shouldComponentUpdate(nextProps, prevState) {
        <span class="hljs-keyword">return</span> !_.isEqual(prevState, <span class="hljs-keyword">this</span>.state);
 }
</code></pre>
<h3 id="t135.4 immutable">5.4 immutable <a href="#t135.4 immutable"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> _ <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;lodash&apos;</span>;
<span class="hljs-keyword">import</span> { is, <span class="hljs-built_in">Map</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;immutable&apos;</span>;
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Caculator</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    state = {
        <span class="hljs-attr">counter</span>: <span class="hljs-built_in">Map</span>({ <span class="hljs-attr">number</span>: <span class="hljs-number">0</span> })
    }
    handleClick = <span class="hljs-function"><span class="hljs-params">()</span> =&gt;</span> {
        <span class="hljs-keyword">let</span> amount = <span class="hljs-keyword">this</span>.amount.value ? <span class="hljs-built_in">Number</span>(<span class="hljs-keyword">this</span>.amount.value) : <span class="hljs-number">0</span>;
        <span class="hljs-keyword">this</span>.setState({ <span class="hljs-attr">counter</span>: <span class="hljs-keyword">this</span>.state.counter.update(<span class="hljs-string">&apos;number&apos;</span>, val =&gt; val + amount) });
    }
    shouldComponentUpdate(nextProps = {}, nextState = {}) {
        nextState = nextState == <span class="hljs-literal">null</span> ? {} : nextState;
        <span class="hljs-keyword">const</span> thisProps = <span class="hljs-keyword">this</span>.props || {}, thisState = <span class="hljs-keyword">this</span>.state || {}, nextState = <span class="hljs-keyword">this</span>.state || {};
        <span class="hljs-keyword">if</span> (<span class="hljs-built_in">Object</span>.keys(thisProps).length !== <span class="hljs-built_in">Object</span>.keys(nextProps).length ||
            <span class="hljs-built_in">Object</span>.keys(thisState).length !== <span class="hljs-built_in">Object</span>.keys(nextState).length) {
            <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
        }

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">in</span> nextProps) {
            <span class="hljs-keyword">if</span> (!is(thisProps[key], nextProps[key])) {
                <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
            }
        }

        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">const</span> key <span class="hljs-keyword">in</span> nextState) {
            <span class="hljs-keyword">if</span> (thisState[key] !== nextState[key] &amp;&amp; !is(thisState[key], nextState[key])) {
                <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
            }
        }
        <span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>;
    }
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.state.counter.get(&apos;number&apos;)}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{input</span> =&gt;</span> this.amount = input} /&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{this.handleClick}</span>&gt;</span>+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        )
    }
}


ReactDOM.render(
    <span class="hljs-tag">&lt;<span class="hljs-name">Caculator</span> /&gt;</span>,
    document.getElementById(&apos;root&apos;)
)
</span></code></pre>
<h2 id="t146. redux+immutable">6. redux+immutable <a href="#t146. redux+immutable"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react&apos;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-dom&apos;</span>;
<span class="hljs-keyword">import</span> PropTypes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;prop-types&apos;</span>
<span class="hljs-keyword">import</span> { createStore, combineReducers, applyMiddleware } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux&apos;</span>
<span class="hljs-keyword">import</span> { Provider, connect } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;react-redux&apos;</span>
<span class="hljs-keyword">import</span> immutable, { is, <span class="hljs-built_in">Map</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;immutable&apos;</span>;
<span class="hljs-keyword">import</span> PureComponent <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;./PureComponent&apos;</span>;

<span class="hljs-keyword">const</span> ADD = <span class="hljs-string">&apos;ADD&apos;</span>;

<span class="hljs-keyword">const</span> initState = <span class="hljs-built_in">Map</span>({ <span class="hljs-attr">number</span>: <span class="hljs-number">0</span> });

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">counter</span>(<span class="hljs-params">state = initState, action</span>) </span>{
    <span class="hljs-keyword">switch</span> (action.type) {
        <span class="hljs-keyword">case</span> ADD:
            <span class="hljs-keyword">return</span> state.update(<span class="hljs-string">&apos;number&apos;</span>, (value) =&gt; value + action.payload);
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> state
    }
}

<span class="hljs-keyword">const</span> store = createStore(counter);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Caculator</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">PureComponent</span> </span>{
    render() {
        <span class="hljs-keyword">return</span> (
            <span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">div</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">p</span>&gt;</span>{this.props.number}<span class="hljs-tag">&lt;/<span class="hljs-name">p</span>&gt;</span>
                <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">ref</span>=<span class="hljs-string">{input</span> =&gt;</span> this.amount = input} /&gt;
                <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">onClick</span>=<span class="hljs-string">{()</span> =&gt;</span> this.props.add(this.amount.value ? Number(this.amount.value) : 0)}&gt;+<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        )
    }
}
let actions = {
    add(payload) {
        return { type: ADD, payload }
    }
}
const ConnectedCaculator = connect(
    state =&gt; ({ number: state.get(&apos;number&apos;) }),
    actions
)(Caculator)

ReactDOM.render(
    <span class="hljs-tag">&lt;<span class="hljs-name">Provider</span> <span class="hljs-attr">store</span>=<span class="hljs-string">{store}</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">ConnectedCaculator</span> /&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">Provider</span>&gt;</span>,
    document.getElementById(&apos;root&apos;)
)
</span></code></pre>
<h2 id="t157. redux-immutable">7. redux-immutable <a href="#t157. redux-immutable"> # </a></h2>
<ul>
<li><a href="https://github.com/gajus/redux-immutable#readme">redux-immutable</a><pre><code class="lang-js"><span class="hljs-keyword">import</span> { combineReducers } <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;redux-immutable&apos;</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">combineReducers</span>(<span class="hljs-params">reducers</span>) </span>{
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">state = Map(</span>), <span class="hljs-title">action</span>) </span>{
      <span class="hljs-keyword">let</span> newState = <span class="hljs-built_in">Map</span>();
      <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> key <span class="hljs-keyword">in</span> reducers) {
          newState = newState.set(key, reducers[key](state.get(key), action));
      }
      <span class="hljs-keyword">return</span> newState;
  }
}
<span class="hljs-keyword">let</span> reducers = combineReducers({
  counter
});
<span class="hljs-keyword">const</span> ConnectedCaculator = connect(
  <span class="hljs-function"><span class="hljs-params">state</span> =&gt;</span> {
      <span class="hljs-keyword">return</span> ({ <span class="hljs-attr">number</span>: state.getIn([<span class="hljs-string">&apos;counter&apos;</span>, <span class="hljs-string">&apos;number&apos;</span>]) })
  },
  actions
)(Caculator)
</code></pre>
</li>
</ul>
<h2 id="t168. react-router-redux">8. react-router-redux <a href="#t168. react-router-redux"> # </a></h2>
<pre><code class="lang-js">import React from &quot;react&quot;;
import ReactDOM from &quot;react-dom&quot;;

import { createStore, applyMiddleware } from &quot;redux&quot;;
import { Provider } from &quot;react-redux&quot;;
import { combineReducers } from &apos;redux-immutable&apos;;
import createHistory from &quot;history/createBrowserHistory&quot;;
import { Route } from &quot;react-router&quot;;
import { Map } from &apos;immutable&apos;;

import {
    ConnectedRouter,
    routerMiddleware,
    push,
    LOCATION_CHANGE
} from &quot;react-router-redux&quot;;

const initialRouterState = Map({
    location: null,
    action: null
});

export function routerReducer(state = initialRouterState, { type, payload = {} } = {}) {
    if (type === LOCATION_CHANGE) {
        const location = payload.location || payload;
        const action = payload.action;

        return state
            .set(&apos;location&apos;, location)
            .set(&apos;action&apos;, action);
    }

    return state;
}
const history = createHistory();

const middleware = routerMiddleware(history);


const store = createStore(
    combineReducers({
        router: routerReducer
    }),
    applyMiddleware(middleware)
);
window.push = push;
window.store = store;
let Home = () =&gt; &lt;div&gt;Home&lt;/div&gt;
let About = () =&gt; &lt;div&gt;About&lt;/div&gt;
let Topics = () =&gt; &lt;div&gt;Topics&lt;/div&gt;
ReactDOM.render(
    &lt;Provider store={store}&gt;
        &lt;ConnectedRouter history={history}&gt;
            &lt;div&gt;
                &lt;Route exact path=&quot;/&quot; component={Home} /&gt;
                &lt;Route path=&quot;/about&quot; component={About} /&gt;
                &lt;Route path=&quot;/topics&quot; component={Topics} /&gt;
            &lt;/div&gt;
        &lt;/ConnectedRouter&gt;
    &lt;/Provider&gt;,
    document.getElementById(&quot;root&quot;)
);
</code></pre>
<h2 id="t179. react-router-redux">9. react-router-redux <a href="#t179. react-router-redux"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> React, { Component } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;react&quot;</span>;
<span class="hljs-keyword">import</span> ReactDOM <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;react-dom&quot;</span>;

<span class="hljs-keyword">import</span> { createStore, combineReducers, applyMiddleware } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;redux&quot;</span>;
<span class="hljs-keyword">import</span> { Provider } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;react-redux&quot;</span>;

<span class="hljs-keyword">import</span> createHistory <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;history/createBrowserHistory&quot;</span>;
<span class="hljs-keyword">import</span> { Router, Route } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;react-router&quot;</span>;
<span class="hljs-keyword">import</span> { Link } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;react-router-dom&quot;</span>;
<span class="hljs-keyword">import</span> PropTypes <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;prop-types&apos;</span>;

<span class="hljs-comment">// import {</span>
<span class="hljs-comment">//     ConnectedRouter,</span>
<span class="hljs-comment">//     routerReducer,</span>
<span class="hljs-comment">//     routerMiddleware,</span>
<span class="hljs-comment">//     push</span>
<span class="hljs-comment">// } from &quot;react-router-redux&quot;;</span>

<span class="hljs-keyword">const</span> CALL_HISTORY_METHOD = <span class="hljs-string">&apos;@@router/CALL_HISTORY_METHOD&apos;</span>;
<span class="hljs-keyword">const</span> LOCATION_CHANGE = <span class="hljs-string">&apos;LOCATION_CHANGE&apos;</span>;
<span class="hljs-keyword">var</span> initialRouteState = {
    <span class="hljs-attr">location</span>: <span class="hljs-literal">null</span>
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ConnectedRouter</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Component</span> </span>{
    <span class="hljs-keyword">static</span> contextTypes = {
        <span class="hljs-attr">store</span>: PropTypes.object
    };

    handleLocationChange = <span class="hljs-function">(<span class="hljs-params">location</span>) =&gt;</span> {
        <span class="hljs-keyword">this</span>.store.dispatch({
            <span class="hljs-attr">type</span>: LOCATION_CHANGE,
            <span class="hljs-attr">payload</span>: location
        });
    }
    componentWillMount() {
        <span class="hljs-keyword">this</span>.store = <span class="hljs-keyword">this</span>.context.store;
        <span class="hljs-keyword">this</span>.history = <span class="hljs-keyword">this</span>.props.history;
        history.listen(<span class="hljs-keyword">this</span>.handleLocationChange);
    }

    render() {
        <span class="hljs-keyword">return</span> &lt;Router {...this.props} /&gt;
    };
}

function routerReducer(state = initialRouteState, action) {
    let { type, payload } = action;
    if (type === LOCATION_CHANGE) {
        return { ...state, location: payload };
    }
    return state;
}

function routerMiddleware(history) {
    return function () {
        return function (next) {
            return function (action) {
                if (action.type !== CALL_HISTORY_METHOD) {
                    return next(action);
                }

                var _action$payload = action.payload,
                    method = _action$payload.method,
                    args = _action$payload.args;
                history[method].apply(history, args);
            };
        };
    };
}
//push
function push(...args) {
    return {
        type: CALL_HISTORY_METHOD,
        payload: { method: &apos;push&apos;, args: args }
    };
}


// Create a history of your choosing (we&apos;re using a browser history in this case)
const history = createHistory();

// Build the middleware for intercepting and dispatching navigation actions
const middleware = routerMiddleware(history);

// Add the reducer to your store on the `router` key
// Also apply our middleware for navigating
const store = createStore(
    combineReducers({
        router: routerReducer
    }),
    applyMiddleware(middleware)
);
window.push = push;
window.store = store;
// Now you can dispatch navigation actions from anywhere!
// store.dispatch(push(&apos;/foo&apos;))
let Home = () =&gt; &lt;div&gt;Home&lt;/div&gt;
let About = () =&gt; &lt;div&gt;About&lt;/div&gt;
ReactDOM.render(
    &lt;Provider store={store}&gt;
        {/* ConnectedRouter will use the store from Provider automatically */}
        &lt;ConnectedRouter history={history}&gt;
            &lt;div&gt;
                &lt;Link to=&quot;/&quot;&gt;Home&lt;/Link&gt;
                &lt;Link to=&quot;/about&quot;&gt;About&lt;/Link&gt;
                &lt;Route exact path=&quot;/&quot; component={Home} /&gt;
                &lt;Route path=&quot;/about&quot; component={About} /&gt;
            &lt;/div&gt;
        &lt;/ConnectedRouter&gt;
    &lt;/Provider&gt;,
    document.getElementById(&quot;root&quot;)
);
</code></pre>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
